package com.blb.blb_erp.realm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.blb_erp.entity.SMenu;
import com.blb.blb_erp.entity.SRole;
import com.blb.blb_erp.entity.SUser;
import com.blb.blb_erp.service.ISMenuService;
import com.blb.blb_erp.service.ISRoleService;
import com.blb.blb_erp.service.ISUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户Realm
 */
public class MyRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获得登录用户
        String username = (String) principalCollection.getPrimaryPrincipal();
        System.out.println("登录用户：" + username);
        //授权信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //添加角色
        info.addRole("role1");
        //添加权限
        info.addStringPermission("user:select");
        info.addStringPermission("user:insert");
        info.addStringPermission("user:delete");
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获得用户输入的账号
        String username = authenticationToken.getPrincipal().toString();
        if(!username.equals("zhang")){
            throw new UnknownAccountException("此用户不存在");
        }
        //验证信息，参数：1、用户名 2、正确密码 3、realm名称
//        return new SimpleAuthenticationInfo(username,"123", getName());
        return new SimpleAuthenticationInfo(username,"44202d045439dc33a2e43d2828d08e19", ByteSource.Util.bytes("007"),getName());
    }
}
